﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Collections.ObjectModel;

namespace Summerset.Isbndb.Publishers
{

    /// <summary>
    /// Represents information about a publisher returned from the publishers collection.
    /// </summary>
    public sealed class PublisherElement : ResponseElementBase
    {
        #region Constructor + Overridden Properties

        /* Example Element:
          <PublisherData publisher_id="dearborn_trade_a_kaplan_profes">
           <Name>Dearborn Trade, a Kaplan Professional Company</Name>
          </PublisherData>          
         */

        /// <summary>
        /// Initializes a new instance of the <see cref="PublisherElement"/> class.
        /// </summary>
        /// <param name="element">The xml element containing the underlying information for this instance.</param>
        public PublisherElement(XmlElement element) 
            : base (element) { }

        /// <summary>
        /// Gets the element name that is expected by the constructor of this object.
        /// If the provided element name does not match the expected element name, an exception will be thrown.
        /// </summary>
        protected override string ExpectedElementName
        {
            get { return "PublisherData"; }
        }

        #endregion
        #region Common Response Properties

        /// <summary>
        /// Gets the ISBNdb-specific id for this category.
        /// </summary>
        public string PublisherId
        {
            get { return _element.Attributes["publisher_id"].Value; }
        }

        /// <summary>
        /// Gets the friendly name for this publisher.  This value can be displayed to users.
        /// </summary>
        public string Name
        {
            get { return ((XmlElement)_element.SelectSingleNode("/Name")).InnerText; }
        }

        #endregion
        #region Additional Response Properties

        /// <summary>
        /// OPTIONAL RESULT: Gets additional details about this instance.
        /// This must be explicitly requested by providing the 'details' constant with the request.
        /// </summary>
        public PublisherElementDetail Details
        {
            get
            {
                if (_details == null)
                {
                    var detailsElement = _element.SelectSingleNode("/Details");

                    if (detailsElement != null)
                        _details = new PublisherElementDetail((XmlElement)detailsElement);
                }

                return _details;
            }
        }
        PublisherElementDetail _details;

        /// <summary>
        /// OPTIONAL RESULT: Gets the categories that this publisher has publications about.
        /// This must be explicitly requested from the API using the 'categories' argument with the request.
        /// </summary>
        public ReadOnlyCollection<PublisherElementCategory> Categories
        {
            get
            {
                if (_categories == null)
                    _categories = base.BuildChildCollection<PublisherElementCategory>("/Categories/Category");

                return _categories;
            }
        }
        ReadOnlyCollection<PublisherElementCategory> _categories;

        #endregion
    }
}
